package com.nt.train.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nt.train.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@Controller
public class TestController {

    //页面跳转方式一
//    访问quick时将自动访问此类中的此方法，若类前有@RequestMapping("/user")，则访问此方法需user/quick
    @RequestMapping("/quick")
    public String save(){
        System.out.println("Controller is running!");
//        若写成forward:success.jsp则表示重定向（不写默认方式）
        //redirect则表示请求转发
        return "success.jsp";
    }
    //页面跳转方式二
    @RequestMapping("/quick2")
    public ModelAndView save1(){
        //mode:封装数据，view展示数据
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","itcast");
        //设置视图名称
        modelAndView.setViewName("success.jsp");
        return modelAndView;
    }
    //方式三
    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView ){
        //mode:封装数据，view展示数据

        //设置模型数据
        modelAndView.addObject("username","小刘");
        //设置视图名称
        modelAndView.setViewName("success.jsp");
        return modelAndView;
    }
    @RequestMapping("/quick4")
    public String save4(Model model){
        model.addAttribute("username","哦豁");
        return "success.jsp";
    }
    //跳转web方式
    @RequestMapping("/quick5")
    public String save5(HttpServletRequest httpRequest){
        httpRequest.setAttribute("username","quick5");
        return "success.jsp";
    }
    //回写数据web方式
    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello 6");

    }
    //回写数据
    @RequestMapping("/quick7")
    @ResponseBody//告诉springMVC框架不要进行页面跳转而是回写字符串
    public String save7() throws IOException {
        return "quick7";
    }
    //回写数据,返回json格式字符串
    @RequestMapping("/quick8")
    @ResponseBody
    public String save8() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":\"41\"}";
    }
    //服务端转json
    @RequestMapping("/quick9")
    @ResponseBody
    public String save9() throws IOException {
        User user = new User();
        user.setUserName("lisi");
        user.setAge(18);
        //使用json的转换工具将对象转换为json格式的字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }
    //期望SpringMVC自动将user转换为json格式的字符串，需要在spring-mvc文件中配置转换器
    @RequestMapping("/quick10")
    @ResponseBody
    public User save10() throws IOException {
        User user = new User();
        user.setUserName("lisi");
        user.setAge(20);

        return user;
    }
    //获得请求参数
    @RequestMapping("/quick11")
    @ResponseBody
    //在地址栏输入?username=xx&age=xx
    public void save11(String username,int age) {
        System.out.println(username);
        System.out.println(age);
    }
    @RequestMapping("/quick12")
    @ResponseBody
    //springMvc自动检测到user类内有何地址栏对应的key和set方法，则地址栏的值会被自动赋给user内的属性
    public void save12(User user) throws IOException {
        System.out.println(user);

    }
    @RequestMapping("/quick13")
    @ResponseBody
    //数组在地址栏输入?strs=xx&strs=xx
    public void save13(String[]strs) throws IOException {
        System.out.println(Arrays.asList(strs));

    }
//    @RequestMapping("/quick14")
//    @ResponseBody
//    //list集合接收,接收
//    public void save14(VO vo) throws IOException {
//        System.out.println(vo);
//
//    }
    @RequestMapping("/quick15")
    @ResponseBody
    //list集合接收,接收前端的ajax数据需要加上@RequestBody
    public void save15(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);

    }
    @RequestMapping("/quick16")
    @ResponseBody
    //将前端名为name的参数数据接收到username中比如在地址栏输入?name=xx
    //required为false时name参数可以不存在，默认为true（不写name参数会报错）
    //defaultValue默认值当name不存在时则会使用此默认值
    public void save16(@RequestParam(value = "name",required = false,defaultValue = "xx") String username) throws IOException {
        System.out.println(username);
    }

    //地址栏输入localhost:8080/quick17/zhangsan，zhangsan会直接被username接收
    @RequestMapping("/quick17/{username}")
    @ResponseBody
    public void save17(@PathVariable(value = "username") String username) throws IOException {
        System.out.println(username);
    }

    //地址栏输入?date=2010/10/10不报错，内部有已有的类型转换器，
    // 而输入2010-10-10格式会报错，所以写自定义转换器

    @RequestMapping("/quick18")
    @ResponseBody
    public void save18(Date date) {
        System.out.println(date);
    }


    //获取请求头信息
    @RequestMapping(value = "/quick19")
    @ResponseBody
    public void save19(@RequestHeader(value = "User-Agent",required = false) String user_agent){
        System.out.println(user_agent);
    }
    //获取cookie，JSESSIONID           设置编码
    @RequestMapping(value = "/quick20",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void save20(@CookieValue(value = "JSESSIONID",required = false) String jsessionid){
        System.out.println(jsessionid);
    }
    //文件上传
    @RequestMapping(value = "/quick21")
    @ResponseBody
    //MultipartFile对象名uploadFile要和表单中type为file的name值一样
    public void save21( String username, @RequestParam(value="uploadFile",required=false)MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        System.out.println(uploadFile);
        //获取上传文件的名称
        //String originalFilename = uploadFile.getOriginalFilename();
        // uploadFile.transferTo(new File("C:\\Users\\张凌潇\\Desktop\\新建文件夹"+originalFilename));

    }


    //多文件上传
    @RequestMapping(value = "/quick22")
    @ResponseBody
    public void save22( String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        System.out.println(uploadFile);
        //多文件上传
        for (MultipartFile multipartFile : uploadFile) {
            //String originalFilename = uploadFile.getOriginalFilename();
            // uploadFile.transferTo(new File("C:\\Users\\张凌潇\\Desktop\\新建文件夹"+originalFilename));

        }
    }
}
